package com.sk.sourcecircle.utils;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sk.sourcecircle.activity.login.LoginActivity;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.callback.HandleDataCallBack;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.LinkedHashMap;
import java.util.Map;

public class HttpUtils {

    public static void postData(String urlStr, final LinkedHashMap<String, Object> args, final HandleDataCallBack callBack, final int tag, final Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        final RequestParams params = new RequestParams(url);
        for (Object o : args.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String val = entry.getValue().toString();
            String key = entry.getKey().toString();
            params.addBodyParameter(key, val);
        }
        long timeStamp = System.currentTimeMillis() / 1000;
        params.setUseCookie(true);
        params.addBodyParameter("timestamp", String.valueOf(timeStamp));
        params.addBodyParameter("sign", MD5.md5(timeStamp + "_yuanquan"));

        if (MyUtils.getToken(MyApplication.getAppContext()) != null && MyUtils.getToken(MyApplication.getAppContext()).length() > 0) {
            params.addBodyParameter("token", MyUtils.getToken(MyApplication.getAppContext()));
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                Log.v("onSuccess", params.toString());
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("code");
                if (code == 200) {
                    callBack.handlerSuccessData(result, tag);
                } else if (code == 401) {
                    SPUtils.clear(activity);
                    Toast.makeText(activity, "您的登录已失效,请您重新登录！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(activity, LoginActivity.class);
                    activity.startActivity(intent);
                } else {
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplication.getAppContext(), jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.getMessage());
                Log.v("onSuccess", params.toString());
                callBack.handleErrorData(ex.getMessage(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    static void getData(String urlStr, final LinkedHashMap<String, Object> args, final HandleDataCallBack callBack, final int tag, final Activity activity) {
        String url = HttpAddress.TestIp + urlStr;
        RequestParams params = new RequestParams(url);
        for (Object o : args.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String val = entry.getValue().toString();
            String key = entry.getKey().toString();
            params.addBodyParameter(key, val);
        }
        long timeStamp = System.currentTimeMillis() / 1000;
        params.setUseCookie(true);
        params.addBodyParameter("timestamp", String.valueOf(timeStamp));
        params.addBodyParameter("sign", MD5.md5(timeStamp + "_yuanquan"));
        if (MyUtils.getToken(MyApplication.getAppContext()) != null && MyUtils.getToken(MyApplication.getAppContext()).length() > 0) {
            params.addBodyParameter("token", MyUtils.getToken(MyApplication.getAppContext()));
        }
        Log.v("getData", params.toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("onSuccess", result);
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("code");
                if (code == 200) {
                    callBack.handlerSuccessData(result, tag);
                } else if (code == 401) {
                    SPUtils.clear(activity);
                    Toast.makeText(activity, "您的登录已失效,请您重新登录！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(activity, LoginActivity.class);
                    activity.startActivity(intent);
                } else {
                    callBack.handlerFailData(result, tag);
                    Toast.makeText(MyApplication.getAppContext(), jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("onError", ex.toString());
                callBack.handleErrorData(ex.toString(), tag);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

}
